﻿using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;

namespace TheOutfield.UmbExt.PageNotifications
{
    public class Environment
    {
        #region Singleton

        private static Environment _instance;
        public string ConfiguredEnvironment { get; set; }

        public static Environment Instance
        {
            get { return _instance ?? (_instance = new Environment()); }
        }

        private Environment()
        {
            ConfiguredEnvironment = GetCurrentEnvironment();
        }

        #endregion

        private static string GetCurrentEnvironment()
        {
            var configuredEnvironment = ConfigurationManager.AppSettings["environment"];
            var ipHostEntry = Dns.GetHostEntry(HttpContext.Current.Request.ServerVariables["HTTP_HOST"]);

            var serverIsLocalIp = (ipHostEntry.AddressList.Where(ip => ip.ToString() == "127.0.0.1").Any());

            var environment = serverIsLocalIp ? "dev" : "live";

            if (string.IsNullOrEmpty(configuredEnvironment) == false)
                environment = configuredEnvironment;
            return environment;
        }
    }
}